package com.tedu.egweiboall.controller;

import com.tedu.egweiboall.mapper.WeiboMapper;
import com.tedu.egweiboall.pojo.dto.WeiboDTO;
import com.tedu.egweiboall.pojo.entity.User;
import com.tedu.egweiboall.pojo.entity.Weibo;
import com.tedu.egweiboall.result.JsonResult;
import com.tedu.egweiboall.result.Status;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@Slf4j
@Api(tags = "微博模块")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;
    @PostMapping("/v1/weibo/insert")
    @ApiOperation("发布微博")
    public JsonResult insert(WeiboDTO weiboDTO, HttpSession session){
        User user = (User)session.getAttribute("user");
        if (user==null){
            return new JsonResult(Status.NOT_LOGIN);
        }
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO,weibo);
        weibo.setUserId(user.getId());
        weibo.setCreated(new Date());
        weiboMapper.insert(weibo);
        return new JsonResult(Status.OPERATION_SUCCESS);
    }

    @PostMapping("v1/weibo/selectIndex")
    @ApiOperation("查询首页微博")
    public JsonResult selectIndex(){
        return new JsonResult(Status.OPERATION_SUCCESS,weiboMapper.selectIndex());
    }

    @PostMapping("/v1/weibo/selectById")
    @ApiOperation("查询微博详情")
    public JsonResult selectById(Integer id) {
        return new JsonResult(Status.OPERATION_SUCCESS, weiboMapper.selectById(id));
    }
}
